package com.kye.express.business.order.scan_code_add_order;

import android.text.TextUtils;

import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.ErrorView;
import com.kye.express.contract.base.view.IView;
import com.kye.express.contract.base.view.LoadingView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.order.scan_code_order.ResponseQuote;
import com.kye.model.bean.order.scan_code_order.ScanCodeAddOrderBean;
import com.kye.model.bean.order.scan_code_order.ScanCodeOrderAddress;
import com.kye.model.bean.order.scan_code_order.request.RequestAddScanCode;
import com.kye.model.bean.user.User;
import com.kye.model.datasource.OrderDataSource;

import java.util.Date;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public interface ScanCodeAddOrderContract {
    interface ScanCodeAddOrderView extends IView, LoadingView, ErrorView {

        void onSendAddress(ScanCodeOrderAddress address);

        void onSuccess(ScanCodeAddOrderBean scanCodeAddOrderBean);

        void onQuote(double quote);
    }

    class ScanCodeAddOrderPresenter extends BasePresenter<ScanCodeAddOrderView> {

        private OrderDataSource mOrderDataSource;

        @Override
        public void bind(ScanCodeAddOrderView view) {
            super.bind(view);

            mOrderDataSource = new OrderDataSource();
        }

        public void checkAddress(ScanCodeOrderAddress address) {
            if (TextUtils.isEmpty(address.getContacts()) || TextUtils.isEmpty(address.getContactsPhone())) {
                UserSession.getInstance()
                        .getUserForRx()
                        .firstOrError()
                        .compose(getView().bindToLifecycleDestroy())
                        .subscribe(new Consumer<User>() {
                            @Override
                            public void accept(User user) throws Exception {
                                address.setContacts(user.getUserName());
                                address.setContactsPhone(user.getPhone());

                                getView().onSendAddress(address);
                            }
                        });
            }
        }


        public void submit(
                String sendTemporary,
                String sendPhone,
                String sendAddress,
                String commodities,
                String serviceMode,
                String remark,
                String reciveTemporary,
                String recivePhone,
                String reciveAddress,
                String sendProvince,
                String sendCity,
                String reciveProvince,
                String reciveCity,
                String supportValue,
                String customerCode,
                String weight,
                String number,
                String wayOfPaying,
                Date goodTime
        ) {
            mOrderDataSource.addScanCode(new RequestAddScanCode(
                    sendTemporary,
                    sendPhone,
                    sendAddress,
                    commodities,
                    serviceMode,
                    remark,
                    reciveTemporary,
                    recivePhone,
                    reciveAddress,
                    sendProvince,
                    sendCity,
                    reciveProvince,
                    reciveCity,
                    supportValue,
                    customerCode,
                    weight,
                    number,
                    wayOfPaying,
                    goodTime
            ))
                    .compose(getView().bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<ScanCodeAddOrderBean>() {
                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<ScanCodeAddOrderBean> result, ScanCodeAddOrderBean scanCodeAddOrderBean) {
                            getView().onSuccess(scanCodeAddOrderBean);
                        }

                        @Override
                        public void onApiError(OpenApiResult<ScanCodeAddOrderBean> result, int code, String msg) {
                            getView().onError(msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onError();
                        }

                        @Override
                        public void onLoaded() {
                            getView().onLoaded();
                        }
                    });
        }

        public void getQuote(
                String customerCode,
                String sendProvince,
                String sendCity,
                String receiveProvince,
                String receiveCity,
                String sendContactsPhone,
                String serviceMode,
                String weight,
                String itemCount) {
            mOrderDataSource.getQuote(
                    customerCode,
                    sendProvince,
                    sendCity,
                    receiveProvince,
                    receiveCity,
                    sendContactsPhone,
                    serviceMode,
                    weight,
                    itemCount
            )
                    .compose(getView().bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<ResponseQuote>() {
                        @Override
                        public void onLoading(Disposable d) {

                        }

                        @Override
                        public void onSuccess(OpenApiResult<ResponseQuote> result, ResponseQuote responseQuote) {
                            getView().onQuote(responseQuote.getQuote());
                        }

                        @Override
                        public void onApiError(OpenApiResult<ResponseQuote> result, int code, String msg) {

                        }

                        @Override
                        public void onOtherError(Throwable e) {

                        }

                        @Override
                        public void onLoaded() {

                        }
                    });
        }
    }
}
